package io.renren.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.entity.*;
import io.renren.service.*;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 员工档案管理(入库表)
 * 
 * @author zhangheng
 * @email zh_123shmily@163.com
 * @date 2017-07-12 11:06:28
 */
@RestController
@RequestMapping("emparchivesbase")
public class EmpArchivesbaseController {
	@Autowired
	private EmpArchivesbaseService empArchivesbaseService;

	@Autowired
	private PersonDispatchService personDispatchService;

	@Autowired
	private CustomSettlementService customSettlementService;

	@Autowired
	private EmpArchivesbaseRecordService empArchivesbaseRecordService;

	@Autowired
	private EmpContractService empContractService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("emparchivesbase:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<EmpArchivesbaseEntity> empArchivesbaseList = empArchivesbaseService.queryList(query);
		int total = empArchivesbaseService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(empArchivesbaseList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("emparchivesbase:info")
	public R info(@PathVariable("id") Integer id){
		EmpArchivesbaseEntity empArchivesbase = empArchivesbaseService.queryObject(id);
		
		return R.ok().put("empArchivesbase", empArchivesbase);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("emparchivesbase:save")
	public R save(@RequestBody EmpArchivesbaseEntity empArchivesbase){
		empArchivesbaseService.save(empArchivesbase);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("emparchivesbase:update")
	public R update(@RequestBody EmpArchivesbaseEntity empArchivesbase){
		empArchivesbaseService.update(empArchivesbase);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("emparchivesbase:delete")
	public R delete(@RequestBody Integer[] ids){
		empArchivesbaseService.deleteBatch(ids);
		
		return R.ok();
	}

	/**
	 * 档案管理接口
	 */
	@RequestMapping("/archivesManage")
	@RequiresPermissions("emparchivesbase:archivesManage")
	public R archivesManage(@RequestBody Integer[] ids){
//		empArchivesbaseService.deleteBatch(ids);
		System.out.println(ids+"-----------------------------");
		List<EmpArchivesbaseEntity> empArchivesbaseEntitys = new ArrayList<>();
		List<Map<String, Object>> personDispatchEntitys = personDispatchService.queryListByIds(ids);
		for (Map<String, Object> map:personDispatchEntitys) {
			String personid = map.get("person_id").toString();
			EmpArchivesbaseEntity empArchivesbaseEntity = empArchivesbaseService.getRecordByPersonid(personid);
			if(empArchivesbaseEntity == null){
				String settlementId = map.get("settlement_id").toString();
				CustomSettlementEntity customSettlementEntity = customSettlementService.queryObject(settlementId);
				empArchivesbaseEntity = new EmpArchivesbaseEntity();
				empArchivesbaseEntity.setEmpName(map.get("name").toString());
				empArchivesbaseEntity.setEmpNo(personid);
				empArchivesbaseEntity.setJobRegForm(customSettlementEntity.getJobRegistration()); //求职登记表
				empArchivesbaseEntity.setCardidCopy(customSettlementEntity.getIdentityCard()); //身份证复印件
				empArchivesbaseEntity.setPhoto(customSettlementEntity.getPicture());//相片
				empArchivesbaseEntity.setLaborContract(customSettlementEntity.getContract());//劳动合同
				empArchivesbaseEntity.setPhysicalExam(customSettlementEntity.getHealthCheckup());//体检表
				empArchivesbaseEntity.setEmpManual(customSettlementEntity.getYgscqsd());//员工手册签收单
				empArchivesbaseEntity.setCheckState("0");
				empArchivesbaseEntitys.add(empArchivesbaseEntity);
			}
		}
		if(empArchivesbaseEntitys.size()>0){
			empArchivesbaseService.saveBatch(empArchivesbaseEntitys);
		}

		return R.ok();
	}

	/**
	 * 查询档案记录表
	 */
	@RequestMapping("/archiveRecord")
	@RequiresPermissions("emparchivesbaserecord:list")
	public R archiveRecord(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);

		List<EmpArchivesbaseRecordEntity> empArchivesbaseRecordList = empArchivesbaseRecordService.queryList(query);
		int total = empArchivesbaseRecordService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(empArchivesbaseRecordList, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}

//	archivePass
	/**
	 * 审核通过
	 */
	@RequestMapping("/archivePass")
	@RequiresPermissions("emparchivesbase:archivePass")
	public R archivePass(@RequestBody Integer[] ids,HttpServletRequest request){
		empArchivesbaseService.passBatch(ids,request.getParameter("status"));

		return R.ok();
	}
	/**
	 * 接收合同证明书
	 */
	@RequestMapping("/acceptance")
	public R acceptance(@RequestBody Integer[] ids,HttpServletRequest request){
		empArchivesbaseService.acceptance(ids,request.getParameter("status"));

		return R.ok();
	}
	/**
	 * 档案异动通过
	 */
	@RequestMapping("/archiveChangePass")
	@RequiresPermissions("emparchivesbase:archiveChangePass")
	public R archiveChangePass(@RequestBody Integer id){
		empArchivesbaseRecordService.pass(id); //审核通过
		EmpArchivesbaseRecordEntity empArchivesbaseRecordEntity = empArchivesbaseRecordService.queryObject(id);
		EmpArchivesbaseEntity empArchivesbase = empArchivesbaseService.queryObject(empArchivesbaseRecordEntity.getEmpArchivesbaseId());

		if ("2".equals(empArchivesbaseRecordEntity.getArchivesbaseCategory()) ){ //如果异动的是合同
                                //使当前合同失效
			Map<String, Object> param = new HashMap();
			param.put("emp_no",empArchivesbase.getEmpNo());
			param.put("if_effective","1");
			List<EmpContractEntity> empContractEntities = empContractService.queryList(param);
			empContractService.disableById(empContractEntities.get(0).getId());

			                    //取得最新合同编号
			Long contractId = empArchivesbaseRecordEntity.getArchivesbaseCategoryId();
			int conId = Integer.parseInt(String.valueOf(contractId));
			empContractService.enableById(conId);//使新合同生效

		}

		return R.ok();
	}


}
